/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modelo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.swing.JOptionPane;

/**
 *
 * @author Ing Arturo
 */
@Entity
public class Representante implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "Cedula")
    private Long id;
    private String nombre;
    private String mail;
    private String rol;
    private String estado;
    private String password;
    @ManyToOne
    private ComitePrograma comiteprograma;
    @OneToMany(mappedBy = "representante")
    private List<HistorialComite> historial;

    public Long getId() {
        return id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public ComitePrograma getComiteprograma() {
        return comiteprograma;
    }

    public void setComiteprograma(ComitePrograma comiteprograma) {
        this.comiteprograma = comiteprograma;
    }

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getRol() {
        return rol;
    }

    public void setRol(String rol) {
        this.rol = rol;
    }

    public String getEstado() {
        return estado;
    }

    public void setEstado(String estado) {
        this.estado = estado;
    }

    public List<HistorialComite> getHistorial() {
        return historial;
    }

    public void setHistorial(List<HistorialComite> historial) {
        this.historial = historial;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Representante)) {
            return false;
        }
        Representante other = (Representante) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Modelo.Representante[id=" + id + "]";
    }

    public void agregarRepresentante(Representante representante) {



        EntityManagerFactory emf = Persistence.createEntityManagerFactory(
                "APU");

        // Crear el EntityManager para accesso a la capa de persistencia
        EntityManager em = emf.createEntityManager();
        // Se es transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n de escritura, obtener la transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n
        // del EntityManager e iniciarla
        em.getTransaction().begin();

        // Marcar el registro en la base de datos
        em.persist(representante);

        // Ejecutar la transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n
        em.getTransaction().commit();

    }

    public Representante findRepresentante(Long cedula) {


        Representante representante = new Representante();

        EntityManagerFactory emf = Persistence.createEntityManagerFactory(
                "APU");

        // Crear el EntityManager para accesso a la capa de persistencia
        EntityManager em = emf.createEntityManager();
        // Se es transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n de escritura, obtener la transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n
        // del EntityManager e iniciarla
        em.getTransaction().begin();

        // Marcar el registro en la base de datos
        representante = em.find(Representante.class, cedula);

        // Ejecutar la transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n
        em.getTransaction().commit();

        return representante;

    }

    public void actualizarRepresentante(Representante representante) {



        EntityManagerFactory emf = Persistence.createEntityManagerFactory(
                "APU");

        // Crear el EntityManager para accesso a la capa de persistencia
        EntityManager em = emf.createEntityManager();
        // Se es transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n de escritura, obtener la transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n
        // del EntityManager e iniciarla
        em.getTransaction().begin();

        // Marcar el registro en la base de datos
        em.merge(representante);

        // Ejecutar la transacciÃƒÆ’Ã†â€™Ãƒâ€ Ã¢â‚¬â„¢ÃƒÆ’Ã¢â‚¬Å¡Ãƒâ€šÃ‚Â³n
        em.getTransaction().commit();

    }

    public List<Representante> getLista(String comite) {

        EntityManagerFactory emf = Persistence.createEntityManagerFactory(
                "APU");

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Query q = em.createQuery("SELECT r FROM Representante r WHERE  r.comiteprograma IS NULL AND r.rol = 'otro'");
        List<Representante> rep = q.getResultList();
        em.getTransaction().commit();
        return rep;
    }

    public List<Representante> getLista2(String comite) {

        EntityManagerFactory emf = Persistence.createEntityManagerFactory(
                "APU");

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Query q = em.createQuery("SELECT r FROM Representante r WHERE  r.comiteprograma.id LIKE :pc  AND r.rol='otro'");
        q.setParameter("pc", comite);
        List<Representante> rep = q.getResultList();
        em.getTransaction().commit();
        return rep;
    }

    public List validaLogin(String id, String clave, String tipo) {
        List l = new ArrayList();
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("APU");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        Query q = null;
        Query q2 = null;
        if (tipo.equals("6")) {
            q = em.createQuery("SELECT r from Representante r WHERE r.rol='director' and r.id=:id  and r.password=:clave and r.comiteprograma IS NOT null");
            q.setParameter("id", Long.parseLong(id));
            q.setParameter("clave", clave);
            try {
                Representante r = (Representante) q.getSingleResult();
                l.add(r);
                System.out.print("entra");
            } catch (Exception ex) {
                l=null;
                System.out.print("nada");
            }
        }
        if (tipo.equals("5")) {
            q = em.createQuery("SELECT r from Representante r WHERE r.password=:clave and r.id=:id ");
            q.setParameter("id", Long.parseLong(id));
            q.setParameter("clave", clave);
          
            try {
                Representante r1 = (Representante) q.getSingleResult();
               
               
                q2 = em.createQuery("SELECT r.presidente from ComiteCentral r");
                Representante aux = (Representante) q2.getSingleResult();
                if (r1.id == aux.id) {
                    l.add(r1);
                 
                } else {
                    
                    q2 = null;
                    q2 = em.createQuery("SELECT r.secretario from ComiteCentral r");
                    aux = (Representante) q2.getSingleResult();
                    if (r1.id == aux.id) {
                        System.out.print("entra");
                        l.add(r1);
                    } else {
                        l=null;
                    }

                }
                } catch  (Exception ex) {
                l=null;
                
            }
        }

        em.getTransaction().commit();


        return l;


    }
}
